package Controle;

import DAO.LoginDAO;
import Modelo.LoginBean;
import java.io.IOException;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name = "logincontroller")
@SessionScoped
public class LoginController implements Serializable {

    private LoginBean lbean;
    
    public LoginController() {
        this.lbean = new LoginBean();
    }

    public void login() throws IOException, Exception {

        LoginDAO ld = new LoginDAO();
        if (ld.ValidarLogin(this.lbean.getUser(), this.lbean.getPass())) {
            FacesContext.getCurrentInstance().getExternalContext().redirect("menu.jsf");
        } else {
            addMessage("Usuário inválido.");
        }
    }

    private void addMessage(String usuário_inválido) {
        //Para chamar <p:growl id="result"/>
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, usuário_inválido,  null);  
        FacesContext.getCurrentInstance().addMessage(null, message); 
    }
    
    public LoginBean getLbean() {
        return this.lbean;  
    }  
  
    public void setLbean(LoginBean value) {  
        this.lbean = value;  
    }  

}